<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改合同')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-tenant-edit" th:object="${sysTenant}" autocomplete="off">
            <input name="tenantId" th:field="*{tenantId}" type="hidden">  <!--租户id-->
            <h4 class="form-header h4">基本信息</h4>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">楼栋：</label>
                    <div class="col-sm-8">
                        <input name="buildingName" th:field="*{buildingName}" class="form-control" type="text" readonly>
                    </div>
<!--                    <div class="col-sm-8">-->
<!--                        <div class="input-group">-->
<!--                            <select id="building" class="form-control" required>-->
<!--                                <option value="" th:text="*{buildingName}"></option>-->
<!--                                <option th:each="building:${building}" th:value="${building.buildingId}" th:text="${building.buildingName}" th:selected="${building.flag}"></option>-->
<!--                            </select>-->
<!--                        </div>-->
<!--                    </div>-->
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">单元号：</label>
                    <div class="col-sm-8">
                      <input name="unitNameStr" th:field="*{unitNameStr}" class="form-control" type="text" readonly>
                    </div>
<!--                    <div class="col-sm-8">-->
<!--                        <div class="input-group">-->
<!--                            <select id="unit" class="form-control noselect2 selectpicker" data-none-selected-text="请选择单元" multiple>-->
<!--                            </select>-->
<!--                        </div>-->
<!--                    </div>-->
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">用途：</label>
                    <div class="col-sm-8">
                        <label class="radio-box"> <input type="radio" name="tenantUse" value="0" th:checked="${sysTenant.tenantUse == '0' ? true : false}"/> 宿舍 </label>
                        <label class="radio-box"> <input type="radio" name="tenantUse" value="1" th:checked="${sysTenant.tenantUse == '1' ? true : false}"/> 个人 </label>
                        <label class="radio-box"> <input type="radio" name="tenantUse" value="2" th:checked="${sysTenant.tenantUse == '2' ? true : false}"/> 企业 </label>
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">租户名称：</label>
                    <div class="col-sm-8">
                        <input name="tenantName" th:field="*{tenantName}" class="form-control" type="text" required readonly>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">面积：</label>
                    <div class="col-sm-8">
                        <input name="tenantArea" th:field="*{tenantArea}" class="form-control" type="text" required readonly>
                    </div>
                </div>
            </div>

            <div class="col-sm-offset-5">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">合同年限：</label>
                    <div class="input-daterange input-group">
                        <input type="text" name="startTime" th:value="${#dates.format(sysTenant.startTime, 'yyyy-MM-dd')}" class="input-sm form-control" id="datetimepicker-startTime" placeholder="yyyy-MM-dd" required readonly/>
                        <span class="input-group-addon">到</span>
                        <input type="text" name="endTime" th:value="${#dates.format(sysTenant.endTime, 'yyyy-MM-dd')}" class="input-sm form-control" id="datetimepicker-endTime" placeholder="yyyy-MM-dd" required readonly/>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">租户状态：</label>
                    <div class="col-sm-8">
                        <label class="radio-box"> <input type="radio" name="tenantStatus" value="0" th:checked="${sysTenant.tenantStatus == '0' ? true : false}" disabled/> 在租 </label>
                        <label class="radio-box"> <input type="radio" name="tenantStatus" value="1" th:checked="${sysTenant.tenantStatus == '1' ? true : false}" disabled/> 提前离租 </label>
                        <label class="radio-box"> <input type="radio" name="tenantStatus" value="2" th:checked="${sysTenant.tenantStatus == '2' ? true : false}" disabled/> 正常离租 </label>
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">租金起算日：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            <input name="rentStartDay" th:value="${#dates.format(sysTenant.rentStartDay, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">每月租金：</label>
                    <div class="col-sm-8">
                        <input name="monthlyRentFee" th:field="*{monthlyRentFee}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">涨租时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            <input name="risePriceFirstTime" id="risePriceFirstTime" onchange="risePriceFirstTimeChange()" th:value="${#dates.format(sysTenant.risePriceFirstTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">涨租比率：</label>
                    <div class="col-sm-7">
                        <input name="risePriceRatio" th:field="*{risePriceRatio}" class="form-control">
                    </div>
                    <div style="float: left"><span style="font-size: 23px">%</span></div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label ">涨租次数：</label>
                    <div class="col-sm-8">
                        <input name="risePriceNumber" th:field="*{risePriceNumber}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">物业费：</label>
                    <div class="col-sm-8">
                        <input name="propertyFee" th:field="*{propertyFee}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                </div>
            </div>

            <div id="leaveDiv" style="display: none">
                <h4 class="form-header h4">离租信息</h4>
                <div class="col-sm-pull-12">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">离租时间：</label>
                        <div class="col-sm-4">
                            <div class="input-group date">
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                <input name="leaveTime" th:value="${#dates.format(sysTenant.leaveTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <h4 class="form-header h4">附加信息</h4>

            <div class="col-sm-offset-6" style="display: none">
                <div class="form-group">
                    <label class="col-sm-3 control-label">涨租状态：</label>
                    <div class="col-sm-8">
                        <label class="radio-box"> <input type="radio" name="risePriceStatus" value="0" checked/> 待涨租 </label>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">押金：</label>
                    <div class="col-sm-8">
                        <input name="pledgeFee" th:field="*{pledgeFee}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">装修押金：</label>
                    <div class="col-sm-8">
                        <input name="decorationPledgeFee" th:field="*{decorationPledgeFee}" class="form-control" type="text">
                    </div>
                </div>
            </div>

            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">清理垃圾费：</label>
                    <div class="col-sm-8">
                        <input name="scavengingFee" th:field="*{scavengingFee}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">水电周转金：</label>
                    <div class="col-sm-8">
                        <input name="utilityFee" th:field="*{utilityFee}" class="form-control" type="text">
                    </div>
                </div>
            </div>

            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">备注：</label>
                    <div class="col-xs-10">
                        <textarea name="remark" th:field="*{remark}" maxlength="500" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script type="text/javascript">
        var prefix = ctx + "system/tenant";
        $("#form-tenant-edit").validate({
            focusCleanup: true
        });

        //涨租时间触发
        function risePriceFirstTimeChange(){
            var risePriceFirstTime = $("#risePriceFirstTime").val();
            var startTime = $("#datetimepicker-startTime").val();
            var endTime = $("#datetimepicker-endTime").val();
            if(risePriceFirstTime!=null && risePriceFirstTime!='' && startTime!=null && startTime!='' && endTime!=null && endTime!=''){
                if(risePriceFirstTime>endTime || risePriceFirstTime<startTime){
                    $("#risePriceFirstTime").val('');
                    $.modal.alert("涨租时间不在合同年限内", modal_status.WARNING);
                    return;
                }
                risePriceNumberChange(startTime,endTime,risePriceFirstTime);
            }
        }

        //自动填写租户涨租次数
        function risePriceNumberChange(startTime,endTime,risePriceFirstTime){
            $.ajax({
                url: prefix + "/risePriceNumberChange",    //请求的url地址
                dataType:"json",   //返回格式为json
                async:true,//请求是否异步，默认为异步，这也是ajax重要特性
                data:{"startTime":startTime,"endTime":endTime,"risePriceFirstTime":risePriceFirstTime},    //参数值
                type:"GET",   //请求方式
                success:function(data){
                    //请求成功时处理
                    $("#risePriceNumber").val(data.data);
                },
                error:function(){
                    //请求出错处理
                }
            });
        }

        function submitHandler() {
            if ($.validate.form()) {
                var data = $('#form-tenant-edit').serializeArray();
                // var buildingId = $.form.selectSelects("building");
                // var buildingName = $('#building').find("option:selected").text();
                // var unitIdStr = $.form.selectSelects("unit");
                // var unitNameStr = new Array();
                // for(var i = 0;i < unitIdStr.split(",").length; i++){
                //     var temp = $('#unit').find("option:selected")[i].innerText;
                //     unitNameStr.push(temp);
                // }
                // data.push({"name": "buildingId", "value": buildingId});
                // data.push({"name": "buildingName", "value": buildingName});
                // data.push({"name": "unitIdStr", "value": unitIdStr});
                // data.push({"name": "unitNameStr", "value": unitNameStr.toString()})
                $.operate.save(prefix + "/edit", data);
            }
        }

        window.onload = function(){
            //如果是离租则展示离租信息
            var tenantStatus = $('input[name="tenantStatus"]:checked').val();
            leaveDivPlay(tenantStatus);

            // //下拉框展示
            // var buildingId = $.form.selectSelects("building");
            // var tenantId = $("#tenantId").val();
            // var unitIdArray = new Array();
            // $('#unit').empty();
            // // 处理自己的业务
            // $.ajax({
            //     url: prefix + "/eidtUnitList",    //请求的url地址
            //     dataType:"json",   //返回格式为json
            //     async:true,//请求是否异步，默认为异步，这也是ajax重要特性
            //     data:{"tenantId":tenantId,"buildingId":buildingId},    //参数值
            //     type:"POST",   //请求方式
            //     success:function(data){
            //         //请求成功时处理
            //         if(data!=null){
            //             for(var i = 0;i < data.data.length; i++){
            //                 var unit = data.data[i];
            //                 $("#unit").append('<option value="'+unit.unitId+'">'+unit.unitName+'</option>');
            //                 if(unit.flag){
            //                     unitIdArray.push(unit.unitId);
            //                 }
            //             }
            //             $('#unit').selectpicker('val', unitIdArray);
            //             $('#unit').selectpicker('refresh');                   // 刷新
            //             $('#unit').selectpicker('render');                   // 渲染
            //         }
            //     },
            //     error:function(){
            //         //请求出错处理
            //     }
            // });
        };

        function leaveDivPlay(tenantStatus){
            if(tenantStatus != '0'){
                document.getElementById("leaveDiv").style.display = "block";
            }
        }

        // $('#building').on('change', function () {
        //     var buildingId = $.form.selectSelects("building");
        //     var tenantId = $("#tenantId").val();
        //     $('#unit').empty();
        //     // 处理自己的业务
        //     $.ajax({
        //         url: prefix + "/eidtUnitList",    //请求的url地址
        //         dataType:"json",   //返回格式为json
        //         async:true,//请求是否异步，默认为异步，这也是ajax重要特性
        //         data:{"tenantId":tenantId,"buildingId":buildingId},    //参数值
        //         type:"POST",   //请求方式
        //         success:function(data){
        //             //请求成功时处理
        //             if(data.data!=null){
        //                 for(var i = 0;i < data.data.size(); i++){
        //                     var unit = data.data[i];
        //                     $("#unit").append('<option value="'+unit.unitId+'">'+unit.unitName+'</option>');
        //                 }
        //             }
        //             else {
        //                 $("#unit").find('option').remove();
        //             }
        //             $('#unit').selectpicker('refresh');                   // 刷新
        //             $('#unit').selectpicker('render');                   // 渲染
        //         },
        //         error:function(){
        //             //请求出错处理
        //         }
        //     });
        // });

        $('#unit').selectpicker({
            'width':'300px',
        });

        $(function() {
            $('#building').select2({
                placeholder: "请选择楼栋",
                allowClear: true
            });
            //进入编辑自动计算涨租次数
            risePriceFirstTimeChange();
        });

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='rentStartDay']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='risePriceFirstTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
</body>
</html>